﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 测试画线
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Ellipse startPoint;
        private Path currentPath;
        private PathGeometry pathGeometry;
        public MainWindow()
        {
            InitializeComponent();
            circuitCanvas.MouseLeftButtonDown += CircuitCanvas_MouseLeftButtonDown;
            circuitCanvas.MouseMove += CircuitCanvas_MouseMove;
            circuitCanvas.MouseLeftButtonUp += CircuitCanvas_MouseLeftButtonUp;
        }
        private void CircuitCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Ellipse clickedEllipse = e.Source as Ellipse;
            if (clickedEllipse != null)
            {
                startPoint = clickedEllipse;
                pathGeometry = new PathGeometry();
                PathFigure pathFigure = new PathFigure();
                pathFigure.StartPoint = new Point(Canvas.GetLeft(startPoint) + startPoint.Width / 2, Canvas.GetTop(startPoint) + startPoint.Height / 2);
                pathGeometry.Figures.Add(pathFigure);
                currentPath = new Path();
                currentPath.Data = pathGeometry;
                currentPath.Stroke = Brushes.Blue;
                currentPath.StrokeThickness = 2;
                circuitCanvas.Children.Add(currentPath);
                circuitCanvas.CaptureMouse();
            }
        }
        private void CircuitCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (circuitCanvas.IsMouseCaptured)
            {
                Point currentMousePosition = e.GetPosition(circuitCanvas);
                if (pathGeometry.Figures.Count > 0)
                {
                    PathFigure pathFigure = pathGeometry.Figures[0];
                    if (pathFigure.Segments.Count > 0)
                    {
                        pathFigure.Segments.RemoveAt(0);
                    }
                    LineSegment lineSegment = new LineSegment(currentMousePosition, true);
                    pathFigure.Segments.Add(lineSegment);
                }
            }
        }
        private void CircuitCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Ellipse endPoint = e.Source as Ellipse;
            if (endPoint != null && endPoint != startPoint)
            {
                if (pathGeometry.Figures.Count > 0)
                {
                    PathFigure pathFigure = pathGeometry.Figures[0];
                    if (pathFigure.Segments.Count > 0)
                    {
                        pathFigure.Segments.RemoveAt(0);
                    }
                    Point endPointPosition = new Point(Canvas.GetLeft(endPoint) + endPoint.Width / 2, Canvas.GetTop(endPoint) + endPoint.Height / 2);
                    LineSegment lineSegment = new LineSegment(endPointPosition, true);
                    pathFigure.Segments.Add(lineSegment);
                }
            }
            else
            {
                circuitCanvas.Children.Remove(currentPath);
            }
            circuitCanvas.ReleaseMouseCapture();
            startPoint = null;
            currentPath = null;
            pathGeometry = null;
        }
    }
}
 